<?php
  require_once('/home/magneto/daemons/daemons.conf.php');
  require_once('/home/magneto/daemons/lib/ses.php');

  // Truchely security
  if ($_POST['AWS_SECRET_KEY'] != AWS_SECRET_KEY) die();


/*
  // Comentar desde aqui
  sleep(rand(2,5));
  error_log("post to: {$_POST['to']} \r\n", 3, '/home/magneto/daemons/die.log');

  // update tries
  file_put_contents(SYNCRO_FOLDER."sends_tries_".$_POST['send_id'],"");

  // update sends
  file_put_contents(SYNCRO_FOLDER."sends_sent_".$_POST['send_id']."_test", "");

  die();
  // Comentar hasta aqui
*/


  $ses = new SimpleEmailService(AWS_USER_KEY, AWS_SECRET_KEY);

  $body = htmlspecialchars_decode($_POST['body'], ENT_QUOTES);
  $mail = new SimpleEmailServiceMessage();
  $mail->addTo($_POST['to']);
  $mail->setFrom($_POST['from']);
  $mail->setReturnPath(RETURN_PATH);
  $mail->setSubject($_POST['subject']);
  $mail->setMessageFromString('Please see html version', $body);


  // update tries
  file_put_contents(SYNCRO_FOLDER."sends_tries_".$_POST['send_id'],"");

  $sendResponse = $ses->sendEmail($mail);

  if (($sendResponse) AND (array_key_exists('MessageId', $sendResponse))) {
    // update sends
    file_put_contents(SYNCRO_FOLDER."sends_sent_".$_POST['send_id']."_".$sendResponse['MessageId'], "");
  } // if ( ($sendResponse) ...

  else {
    $now = date("Y-m-d H:i:s") . substr((string)microtime(), 1, 8);
    error_log("{$now} #### Error #### while sending to {$_POST['to']}, campaign {$_POST['campaign_id']} (RequestId = {$sendResponse['RequestId']})\r\n", 3, LOG_FILE);
  }
